/**
 * Created by jun on 2018/3/24.
 */
var logManager=require("../Logs.js");
var BaseAnalysis=require("../protocol/BaseProtocolAnalysis")
var SystemEvent=require("../event/SystemEvent")
var TcpSocketServerClient=Class.extend({
    id:0,
    port:0,
    connection:null,
    analysis:null,
    eventManager:null,
    sendMsg:function(msg,data){
        if(typeof msg !=="number"){
            console.log("tcpsocket send msg should be a number,you can set obj or string in data")
            return
        }
        logManager.logP(`send tcp protocol:id=${this.id},port=${this.port},msg=${msg}`)
        // 申请一个头用于设置
        this.connection.write(this.analysis.package(msg,data));
        // this.connection.end()
    },
    ctor:function(id,conn,port,analysisClass,eventManager){
        const cls = analysisClass||BaseAnalysis;
        this.id=id;
        this.port=port;
        this.connection=conn;
        this.analysis=new cls();
        this.eventManager=eventManager;
        conn.on("data",this.onData.bind(this));
        conn.on("close",this.onClose.bind(this));
        conn.on("error",this.onError.bind(this));
    },
    closeSocket:function(){
        try{
            this.connection.destroy();
            // this.connection.close();
        }catch(e){
            logManager.logM(e);
        }
        this.eventManager.dispatchEvent(SystemEvent.TCP_SOCKET_CLOSE,[{clientId:this.id,port:this.port}]);
    },
    onError:function (code, reason) {
        logManager.logM(`连接错误：id=${this.id},port=${this.port}`);
        this.eventManager.dispatchEvent(SystemEvent.TCP_SOCKET_CLOSE,[{clientId:this.id,port:this.port}]);
    },
    onClose:function (code, reason) {
        logManager.logM(`连接关闭：id=${this.id},port=${this.port}`);
        this.eventManager.dispatchEvent(SystemEvent.TCP_SOCKET_CLOSE,[{clientId:this.id,port:this.port}]);
    },
    onData:function(data){
        logManager.logP(`recive tcp data:id=${this.id},port=${this.port},len=${data.length}`);
        var list=this.analysis.unPackage(data);
        logManager.logP("recive tcp protocols:"+(list?list.length:0));
        for(var i=0;i<list.length;i++){
            var result=list[i]
            // result.clientId=this.id;
            // result.port=this.port;
            if(result.error){
                logManager.logP("protocol error:"+result.error+JSON.stringify(result));
                const preventDefault=this.eventManager.dispatchEvent(SystemEvent.TCP_SOCKET_DATA_PACKAGE_ERROR,[{error:result,clientId:this.id,port:this.port}])
                if(!preventDefault){
                    logManager.logP("protocol error default handler close socket:"+result.error+JSON.stringify(result));
                    this.closeSocket();
                }
            }else{
                logManager.logP("dispatch tcp protocol:"+result.type);
                this.eventManager.dispatchEvent(SystemEvent.TCP_SOCKET_DATA,[{type:result.type,data:result.data,clientId:this.id,port:this.port}]);
            }
        }
        logManager.logP("dispatch tcp protocol:finish");
    }
})
module.exports=TcpSocketServerClient;